from django.shortcuts import render
from rest_framework.views import APIView
from .models import User
from rest_framework.response import Response
from rest_framework.generics import CreateAPIView
from .serializers import CreateUserSerializer,EmailSerialzier
from rest_framework.generics import RetrieveAPIView,UpdateAPIView
from rest_framework import status


# Create your views here.

#APView
#GenericAPIView
#ListAPIView,CreateAPIView...
class RegisterUsernameView(APIView):
    """
    校验用户名是否放注册过

    GET     /users/usernames/(?P<username>\w{5,20})/count/


    拿到用户名之后? 我如何判断 这个用户名是否注册过? 查询数据库
    """
    def get(self,request,username):
        # 查询用户名
        #
        count = User.objects.filter(username=username).count()
        # 组织数据
        context = {
            'count':count,
            'username':username #可选
        }
        # 返回响应
        return Response(context)

class RegisterMobileView(APIView):

    """
    判断手机号是否注册

    GET      /users/phones/(?P<mobile>1[345789]\d{9})/count/
    """

    def get(self,request,mobile):

        count = User.objects.filter(mobile=mobile).count()

        context = {
            'count':count,
            'mobile':mobile
        }

        return Response(context)

#APIView

#GenericAPIView
#ListAPIView,CreateAPIView...

# 数据入库
class CreateUserView(CreateAPIView):
    """
    实现注册功能
    POST        /users/

    1. 我们需要把 用户名 密码,确认密码,手机号,短信验证码, 是否同意,提交给后台
    2. 对数据进行校验
    3. 数据入库

    """
    serializer_class = CreateUserSerializer


# APIView
# GenericAPIView
# LlistAPIView,RetrieveAPIView
from rest_framework.permissions import IsAuthenticated
from .serializers import UserCenterInfoSerializer

# class UserCenterInfoView(APIView):

    # """
    # GET /users/infos/
    #
    # 1. 获取用户的信息,用户将用户信息 序列化
    # 2. 个人中心 必须为登录用户
    # """
    #
    # permission_classes = [IsAuthenticated]
    #
    # def get(self,request):
    #
    #     serializer = UserCenterInfoSerializer(request.user)
    #     return Response(serializer.data)


# APIView
# GenericAPIView
# LlistAPIView,RetrieveAPIView

class UserCenterInfoView(RetrieveAPIView):

    """
    GET /users/infos/

    1. 获取用户的信息,用户将用户信息 序列化
    2. 个人中心 必须为登录用户
    """

    permission_classes = [IsAuthenticated]

    serializer_class = UserCenterInfoSerializer

    def get_object(self):

        return self.request.user



#APIView
# GenericAPIView
# UpdateAPIView
class EmailView(UpdateAPIView):

    """邮件
    1. 登录用户显示页面
    2. 当我们点击保存的时候,把邮箱信息发送过来,我们更新数据库的数据,更新指定用户
    3. 我们需要发送 验证码邮件

    """
    permission_classes = [IsAuthenticated]

    serializer_class = EmailSerialzier

    def get_object(self):

        return self.request.user

class VerifyEmailView(APIView):

    def get(self,request):

        # 获取token
        token = request.query_params.get('token')
        # 判断token是否存在
        if token is None:
            return Response(status=status.HTTP_400_BAD_REQUEST)

        # 对token进行loads操作
        user = User.check_verify_token(token)
        if user is None:
            return Response(status=status.HTTP_400_BAD_REQUEST)

        # 修改用户的 email_active 状态

        user.email_active = True
        user.save()
        return Response({'message':'ok'})
